/**
 * TODO
 *
 * @ClassName: HelloController
 * @author: changhong
 * @since: 2020/5/14 15:06
 */
package life.changhongdeshijie.community.demo.controller;
import life.changhongdeshijie.community.demo.mapper.UserMapper;
import life.changhongdeshijie.community.demo.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

@Controller()
public class IndexController {
    @Autowired
    private UserMapper userMapper;
    @GetMapping("/")
    public String index(HttpServletRequest request){
        Cookie[] cookies=request.getCookies();
        for ( Cookie cookie:
             cookies) {
            if (cookie.getName().equals("token")){
                String token=cookie.getValue();
                User user=userMapper.fingByToken(token);
                if (user!=null){
                    request.getSession().setAttribute("user",user);
                }
                break;
            }
        }

        return "index";
    }
}
